/**
 * 
 */
package org.istt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.istt.dao.MenuDao;
import org.istt.domain.Menu;
import org.istt.model.MenuTreeBean;
import org.istt.service.MenuManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author NDTHO
 *
 */
@Service("menuManager")
public class MenuManagerImpl extends ManagerImpl<Menu, Long> implements MenuManager {
	
	MenuDao menuDao;
	
	/**
	 * @param menuDao the menuDao to set
	 */
	@Autowired
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

	@Override
	public List<Menu> getMenuTrees() {
		
		List<Menu> lstMenu = new ArrayList<Menu>();
		List<MenuTreeBean> lstMenuBean = this.menuDao.getMenuTrees();
		for (int i = 0; i < lstMenuBean.size(); i++) {
			MenuTreeBean mnu = lstMenuBean.get(i);
			
			lstMenu.add(mnu.getMainMenu());
			
			if(mnu.getLstSubMenu()!=null && mnu.getLstSubMenu().size()!=0){
				for (int j = 0; j < mnu.getLstSubMenu().size(); j++) {
					Menu submenu = mnu.getLstSubMenu().get(j);
					lstMenu.add(submenu);
				}
			}
		}
		return lstMenu;
	}

	@Override
	public Menu getMenuById(String menuId) {
		
		return this.menuDao.getMenuById(Long.parseLong(menuId));
	}

	@Override
	public void saveMenu(Menu menu) {
		
		System.out.println("===== save menu: | Isfather="+menu.getIsFather());
		this.menuDao.saveMenu(menu);
		
	}

	@Override
	public void removeMenu(String menuId) {
		
		this.menuDao.removeMenu(Long.parseLong(menuId));
	}
	
	@Override
	public List<Menu> getList() {
		return getMenuTrees();
	}

	@Override
	public List<MenuTreeBean> getListMenuBeans() {
		
		return this.menuDao.getMenuTrees();
	}

	@Override
	public List<Menu> getFatherMenu() {
		// TODO Auto-generated method stub
		
		return this.menuDao.getFatherMenu();
	}

	
	

}
